<?php
/*
 * Created on 03.01.2006
 *
 */
 
/**
 * id -> $actionform
 * globalScope -> $env->getGlobal
 * requestScope -> $env->getParam
 * serverScope -> $env->getServer
 * cookieScope -> $env->getCookie
 * fileScope -> $env->getFile
 * envScope -> $env->getEnv
 * sessionScope -> $env->getSession
 * responseScope -> $env->getAttribute
 */
class ELCallback
{

    var $tokens = array();
    var $code = '';

    function id ()
    {
        $dump = '';
        if (count($this->tokens)>0)
        {
            $first = array_shift($this->tokens);
            if ($first == '$')
            {
                $dump = '$'.$this->tokens[0];
            }
            else
            {
                if (count($this->tokens)>0)
                {
                    switch ($first)
                    {
                        case 'globalScope':
                            $dump = '$env->getGlobal("'.$this->tokens[0].'")';
                            break;
                        case 'requestScope':
                            $dump = '$env->getParam("'.$this->tokens[0].'")';
                            break;
                        case 'serverScope':
                            $dump = '$env->getServer("'.$this->tokens[0].'")';
                            break;
                        case 'cookieScope':
                            $dump = '$env->getCookie("'.$this->tokens[0].'")';
                            break;
                        case 'fileScope':
                            $dump = '$env->getFile("'.$this->tokens[0].'")';
                            break;
                        case 'envScope':
                            $dump = '$env->getEnv("'.$this->tokens[0].'")';
                            break;
                        case 'sessionScope':
                            $dump = '$env->getSession("'.$this->tokens[0].'")';
                            break;
                        case 'responseScope':
                            $dump = '$env->getAttribute("'.$this->tokens[0].'")';
                            break;
                        default:
                            array_unshift($this->tokens,$first);
                            $dump = 'getNestedValue($actionform, '.var_export($this->tokens, true).')';
                            break;
                    }
                }
                else 
                {
                    $dump = '$actionform->get("'.$first.'")';
                }
            }
            $this->tokens = array();
            return $dump;
        }    
    }
 
    function shift(&$token, $stack)
    {
        if ($token['type'] == 'outerstring')
        {
            $this->code .= $this->id ($this->tokens);
            $this->code .= "'".addslashes($token['value'])."'.";
        }
        elseif ($token['type'] == 'string')
        {
            $this->code .= $this->id ($this->tokens);
            $this->code .= preg_replace('/\\\\n/', 'n', '"'.addslashes($token['value']).'"');
        }
        elseif ($token['type'] == 'dollar')
        {
            $this->tokens[] = $token['value'];
        }
        elseif ($token['type'] == 'id' || $token['type'] == 'arrow')
        {
            if ($token['type'] == 'id')
                if ($this->isFun)
                {
                    $this->isFun = false;
                    if (!function_exists($token['value']))
                    {
                        die ("<b>EL Error:</b> Function ".$token['value']." does not exist in line ".$token['line']."\n");
                    }
                    $this->code .= $token['value'];
                }
                else
                    $this->tokens[] = $token['value'];
        }
        elseif ($token['type'] == 'codeopen')
        {
            $this->code .= $this->id ($this->tokens);
            $this->code .= "(";
        }
        elseif ($token['type'] == 'rcurlybrace')
        {
            $this->code .= $this->id ($this->tokens);
            $this->code .= ").";
        }
        elseif ($token['type'] == 'eof')
        {
            $this->code = substr($this->code,0 , strlen($this->code)-1);
        }
        elseif ($token['type'] == 'fun')
        {
            $this->isFun = true;
        }
        else
        {
            $this->code .= $this->id ($this->tokens);
            $this->code .= $token['value'];
        }
    }
    
    function reduce ($no, $rule, $stack)
    {
    }
    
    function getCode()
    {
        return $this->code;
    }
    
    function reset()
    {
        $this->code = '';
    }
    
}
?>
